
package com.czy.basecommon.utils.tools;

import android.annotation.SuppressLint;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author  Jake Han
 */
public class TimeTool {
    public static final String format1="yyyy-MM-dd HH:mm:ss";
    public static final String format2="yyyy/MM/dd";
    public static final String format3="yyyyMMddHHmmss";
    public static final String format4="yyyy-MM-dd";
    public static final String format5="yyyy/MM/dd HH:mm:ss";
    public static final String format6="HH:mm";
    public static final String format7="yyyyMMddHHmm";
    public static final String format8 = "MM-dd HH:mm:ss";
    private static final String TAG = "TimeUtil";
    @SuppressLint("SimpleDateFormat")
    public static String getCunrrentDateString(){
        SimpleDateFormat formatter = new SimpleDateFormat(format1);
        String dateString = formatter.format(new Date());
        return dateString;
    }
    
    @SuppressLint("SimpleDateFormat")
    public static long String2Long(String date,String format) throws ParseException{
        SimpleDateFormat formatter=new SimpleDateFormat(format);
        long timeMiilis=formatter.parse(date).getTime();
        return timeMiilis;
    }
    
    @SuppressLint("SimpleDateFormat")
	public static String format(long timeMiss,String format)throws ParseException{
    	SimpleDateFormat formatter = new SimpleDateFormat(format);
    	String dateString = formatter.format(timeMiss);
		return dateString;
    }
    
    /**
	 * 将时间戳转为字符串 Added by Danny 2013/07/29
	 * @param lngTime:long时间
	 * @param formatType:需要格式化的时间类型
	 */
    @SuppressLint("SimpleDateFormat")
 	public static String getStringDateTime(long lngTime,String formatType) {
 		
 		if(lngTime<0) return "";
 		if("".equals(formatType))
 			formatType=format1;
 		
 		SimpleDateFormat sdf = new SimpleDateFormat(formatType);
 		Date mDate=new Date(lngTime);
 		String  mStrTime = sdf.format(mDate);
 		return mStrTime;
 	}
    
    public static long stringTimeToLongTime(String strTime){
    	String[] times=strTime.split(":");
    	return (Integer.valueOf(times[0])*3600+Integer.valueOf(times[1])*60)*1000;
    }
    
    
    public static String longTimeToStringTime(long lngTime){
    	return String.valueOf(lngTime/60000);
    }
      
    public static String getTimeFromDateTime(String strDateTime){
    	Log.i(TAG, "strDateTime:"+strDateTime);
    	if(14!=strDateTime.length()) return "";
    	try{
	    	String strYear=strDateTime.substring(0,4);
	    	Log.i(TAG, "Year:"+strYear);
	    	String strMonth=strDateTime.substring(4,6);
	    	Log.i(TAG, "Month:"+strMonth);
	    	String strDay=strDateTime.substring(6,8);
	    	Log.i(TAG, "Day:"+strDay);
	    	String strHour=strDateTime.substring(8,10);
	    	Log.i(TAG, "Hour:"+strHour);
	    	String strMenite=strDateTime.substring(10,12);
	    	Log.i(TAG, "Menite:"+strMenite);
	    	String strSecond=strDateTime.substring(12,14);
	    	Log.i(TAG, "Second:"+strSecond);
	    	return strHour+":"+strMenite;
    	}catch(Exception e){
    		e.printStackTrace();
    		return "";
    	}
    }
    
    public static long stringDateTimeToLongTime(String strDateTime){
    	Log.i(TAG, "strDateTime:"+strDateTime);
    	if(14!=strDateTime.length()) return 0;
    	try{
	    	String strYear=strDateTime.substring(0,4);
	    	Log.i(TAG, "Year:"+strYear);
	    	String strMonth=strDateTime.substring(4,6);
	    	Log.i(TAG, "Month:"+strMonth);
	    	String strDay=strDateTime.substring(6,8);
	    	Log.i(TAG, "Day:"+strDay);
	    	String strHour=strDateTime.substring(8,10);
	    	Log.i(TAG, "Hour:"+strHour);
	    	String strMenite=strDateTime.substring(10,12);
	    	Log.i(TAG, "Menite:"+strMenite);
	    	String strSecond=strDateTime.substring(12,14);
	    	Log.i(TAG, "Second:"+strSecond);
	    	
	    	String dateTime=strYear+"-"+strMonth+"-"+strDay+" "+strHour+":"+strMenite+":"+strSecond;
	    	Log.i(TAG, "stringDateTimeToLongTime:"+dateTime);
	    	return String2Long(dateTime,format1);
    	}catch(Exception e){
    		e.printStackTrace();
    		return 0;
    	}
    }

    public static long getDiffDays(long lngLastUpdateDateTime, long lngCurrentSystemTime) {
        long day = 0;
        try {
	        String endDateStr=TimeTool.format(lngCurrentSystemTime, TimeTool.format4);
	        
	        String beginDateStr=TimeTool.format(lngLastUpdateDateTime, TimeTool.format4);
	        Log.i(TAG, "beginDateStr:"+beginDateStr);
	        Log.i(TAG, "endDateStr:"+endDateStr);
	        
	        SimpleDateFormat format = new SimpleDateFormat(
	                "yyyy-MM-dd");
	        Date beginDate;
	        Date endDate;
        
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime())
                    / (24 * 60 * 60 * 1000);
            // System.out.println("相隔的天数="+day);
        } catch (ParseException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return day;
    }
}
